require "Net/send_Msg"
require "Net/TrialHandel"
require 'View/UIBase/UIBase'

UIMythicDungeonsReward = UIBase.New(PanelNames.UIMythicDungeonsReward);
UIMythicDungeonsReward.id = 0;
function UIMythicDungeonsReward.Init( rewardId)
    UIMythicDungeonsReward.id = rewardId;
    UIBase.Init(UIMythicDungeonsReward);
end

--定义控件
function UIMythicDungeonsReward:OnCreate()
    self.rewardLevels = {  "S", "A", "B", "C", "D"};
    self.rewardList = self:GetChild("list");
    self.closeBgBtn = self:GetChild("btnBack");
    self.bgBack = self:GetChild("bgBack")
    self:addEvent();

    --self.CloseFom();
end

function UIMythicDungeonsReward:OnEnable()
    self:refresh();
end

function UIMythicDungeonsReward:OnDisable()

end

function UIMythicDungeonsReward:refresh()
    self.cfg = G.dataTable["t_mythicdungeonsdrop"][tostring(UIMythicDungeonsReward.id)];
    self.rewardList.itemRenderer = self.rendererItem;
    self.rewardList.numItems = #self.rewardLevels;
end

function UIMythicDungeonsReward:OnDestroy()
end

function UIMythicDungeonsReward:addEvent()
    self:RegisterOnClick(self.closeBgBtn, self.onCloseClick)
    self:RegisterOnClick(self.bgBack, self.onCloseClick)
end

function UIMythicDungeonsReward:removeEvent()

end

function UIMythicDungeonsReward:onCloseClick()
    closeUI(PanelNames.UIMythicDungeonsReward);
end

function UIMythicDungeonsReward.rendererItem(idx, obj)
    self = UIMythicDungeonsReward;
    local levelController = obj:GetController("rank");
    local title = obj:GetChild("title");
    levelController.selectedIndex = idx;
    local listReward = obj:GetChild("listReward");
    local listRewardRandom = obj:GetChild("listRewardRandom");

    local index = idx + 1;
    title.text = getLanguage("MythicDungeons_tips"..index);
    local strLevel = self.rewardLevels[index];
    self.rewardCfg = decodeJsonStrToTabel(self.cfg["f_RewardRes" .. strLevel]);
    listReward.itemRenderer = self.rendererRewardItem;
    listReward.numItems = #self.rewardCfg;
    self.dropRewardCfg = decodeJsonStrToTabel(self.cfg["f_RewardEquipShow" .. strLevel]);
    if self.dropRewardCfg then
        listRewardRandom.itemRenderer = self.rendererRandomRewardItem;
        listRewardRandom.numItems = #self.dropRewardCfg;
    else
        listRewardRandom.visible = false
    end
end

--必掉道具
function UIMythicDungeonsReward.rendererRewardItem(idx, obj)
    self = UIMythicDungeonsReward;
    local index = idx + 1;
    local cfg = self.rewardCfg[index];
    local data = logicMgr.ItemManager.getItemConstDatabyModelId(cfg[1]);
    data.num = cfg[2];
    local itemCtr = CtrlManager.GetCtrl(PanelNames.Item);
    itemCtr.initData(obj, data);
end

--随机掉落的道具
function UIMythicDungeonsReward.rendererRandomRewardItem(idx, obj)
    self = UIMythicDungeonsReward;
    local index = idx + 1;
    local cfg = self.dropRewardCfg[index];
    local data = {}
    data.num = "";
    data.itemModelId = cfg
    local itemCtr = CtrlManager.GetCtrl(PanelNames.Item);
    itemCtr.initData(obj, data);
end

return UIMythicDungeonsReward;